/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.course;


import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.config.Global;
import com.idenger.common.utils.StringUtils;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.EduCourse;
import com.idenger.modules.edu.entity.course.EduCourseTeacher;
import com.idenger.modules.edu.entity.project.EduProjectCourse;
import com.idenger.modules.edu.entity.teacher.EduTeacherInfo;
import com.idenger.modules.edu.service.course.EduCourseTeacherService;
import com.idenger.modules.edu.service.project.EduProjectCourseService;
import com.idenger.modules.edu.service.teacher.EduTeacherInfoService;
import com.idenger.modules.edu.web.comm.Constant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.persistence.Page;
import com.idenger.modules.edu.service.course.EduCourseService;

/**
 * 课程Controller
 * @author chenlin
 * @version 2017-11-29
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/course/eduCourse")
public class EduCourseController extends BaseController {

	@Autowired
	private EduCourseService eduCourseService; 
	
	@Autowired
	private EduCourseTeacherService eduCourseTeacherService;
	
	@Autowired
	private EduTeacherInfoService eduTeacherInfoService;
	
	@Autowired
	private EduProjectCourseService eduProjectCourseService;
	
	
	@ModelAttribute
	public EduCourse get(@RequestParam(required=false) String id) {
		EduCourse entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduCourseService.get(id);
		}
		if (entity == null){
			entity = new EduCourse();                                 
		}
		return entity;
	}
	
	@RequiresPermissions("edu:course:eduCourse:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduCourse eduCourse, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduCourse> page = eduCourseService.findPage(new Page<EduCourse>(request, response), eduCourse); 
		model.addAttribute("page", page);
		return "modules/edu/course/eduCourseList";
	}
	/*
	 *添加课程的时候，查询教师的列表数据，以及搜索的接口
	 *返回json数据 页面进行渲染
	 *
	 * **/
	
	@RequiresPermissions("edu:course:eduCourse:view")
	@RequestMapping(value = "selCourseList")
	public String allCourseList(EduCourse eduCourse, HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		String name = request.getParameter("name");
		if(StringUtils.isNotBlank(name)){
			eduCourse.setName(name);
			model.addAttribute("name",name);
		}else{
			eduCourse.setName("");
		}
		List<EduCourse> list = eduCourseService.findList(eduCourse);
		if(list.size()>0){
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData("eduCourseList",list);
		}else{
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("暂时没有课程");
		}
		return renderString(response, baseResponseMsg);
	}
	@RequiresPermissions("edu:course:eduCourse:view")
	@RequestMapping(value = "getCourse")
	public String getCourse(EduCourse eduCourse, HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		
		if(StringUtils.isNotBlank(eduCourse.getVideoScore())){
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData("eduCourse",eduCourse);	
			baseResponseMsg.addData("result","update");	
		}else{
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			EduCourse eduCourse1=new EduCourse();
			baseResponseMsg.addData("eduCourse",eduCourse1);	
			baseResponseMsg.addData("result","add");	
		}
		return renderString(response, baseResponseMsg);
	}
	
	
	
	@RequiresPermissions("edu:course:eduCourse:view")
	@RequestMapping(value = "courseIfBelongToProject")
	public String courseIfBelongToProject(EduCourse eduCourse, HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;	
		EduProjectCourse eduProjectCourse = new EduProjectCourse();
		eduProjectCourse.setCourseId(eduCourse.getId());
		List<EduProjectCourse> elist = eduProjectCourseService.courseIfBelongToProjectByCourseId(eduProjectCourse);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		if(elist.size()>0){
			baseResponseMsg.addData("result",elist.size());
		}else{
			baseResponseMsg.addData("result",0);
		}
		return renderString(response, baseResponseMsg);
	}
	
	
	@RequiresPermissions("edu:course:eduCourse:view")
	@RequestMapping(value = "form")
	public String form(EduCourse eduCourse, Model model) {
		//根据课程ID查询教师信息
		List<EduTeacherInfo> eduTeacher = eduTeacherInfoService.findCourseTeacherDetailByCourseId(eduCourse.getId());
		if(eduTeacher!=null && eduTeacher.size()>0){
			eduCourse.setEduTeacher(eduTeacher);
		}
		//查询所有教师
		EduTeacherInfo eduTeacherInfo = new EduTeacherInfo();
		List<EduTeacherInfo> teacherInfoList = eduTeacherInfoService.findList(eduTeacherInfo);

		model.addAttribute("teacherInfoList", teacherInfoList);
		
		model.addAttribute("eduCourse", eduCourse);
		
		return "modules/edu/course/eduCourseForm";
	}

	@RequiresPermissions("edu:course:eduCourse:edit")
	@RequestMapping(value = "save")
	public String save(EduCourse eduCourse, Model model, RedirectAttributes redirectAttributes) {
		if(StringUtils.isBlank(eduCourse.getId())){
			eduCourse.setStatus(Constant.COURSE_STATUS_NO);//课程未上线
		}
		//优惠价设置小数
		if(StringUtils.isNotBlank(eduCourse.getPreferentialPrice())){
			//不为空
			BigDecimal pp = new BigDecimal(eduCourse.getPreferentialPrice());
			pp.setScale(2,BigDecimal.ROUND_HALF_UP);
			eduCourse.setPreferentialPrice(pp.toString());

		}else{
			//如果保存课程的时候不填写价格
			BigDecimal pp = new BigDecimal("0.00");
			pp.setScale(2);
			eduCourse.setPreferentialPrice(pp.toString());
		}
		//原价
		if(StringUtils.isNotBlank(eduCourse.getPrice())){
			BigDecimal p = new BigDecimal(eduCourse.getPreferentialPrice());
			p.setScale(2,BigDecimal.ROUND_HALF_UP);
			eduCourse.setPrice(p.toString());
		}else{
			BigDecimal p = new BigDecimal("0.00");
			p.setScale(2);
			eduCourse.setPrice(p.toString());
		}
		//学时学分为空的时候设置为0
		if(StringUtils.isBlank(eduCourse.getScore())){
			eduCourse.setScore("0");
		}
		if(StringUtils.isBlank(eduCourse.getStartPeriod())){
			eduCourse.setStartPeriod("0");
		}
		eduCourseService.save(eduCourse);
		
		List<EduTeacherInfo> listTeacherInfo = eduCourse.getEduTeacher();

		if(listTeacherInfo!=null && listTeacherInfo.size()>0){
			//删除所有的课程里的教师教师
			EduCourseTeacher courseTeacher = new EduCourseTeacher();
			courseTeacher.setCourseId(eduCourse.getId());
			List<EduCourseTeacher> eduCourseTeacherList = eduCourseTeacherService.findTeacherListByCourseId(courseTeacher);
			for(EduCourseTeacher eduCourseTeacher : eduCourseTeacherList){
				eduCourseTeacherService.delete(eduCourseTeacher);
			}
			for (EduTeacherInfo eduTeacherInfo : listTeacherInfo) {
				//列表传		teacherId  教师ID
				EduCourseTeacher eduCourseTeacher = new EduCourseTeacher();
				eduCourseTeacher.setType(Constant.COURSE_TEACHER_TYPE_COURSE);//课程类型
				eduCourseTeacher.setCourseId(eduCourse.getId());
				eduCourseTeacher.setTeacherId(eduTeacherInfo.getId());
				eduCourseTeacherService.save(eduCourseTeacher);
			}
		}
		model.addAttribute("skip","true");
		return "redirect:"+ Global.getAdminPath()+"/edu/course/eduSection/list?courseId="+eduCourse.getId();
	}
	
	
	//设置分值
	@RequiresPermissions("edu:course:eduCourse:edit")
	@RequestMapping(value = "saveScore")
	public String saveScore(EduCourse educourse, Model model, HttpServletRequest request, HttpServletResponse response,RedirectAttributes redirectAttributes) {
		BaseResponseMsg baseResponseMsg;
		//修改分值
		System.out.println(educourse.getScore());
		eduCourseService.save(educourse);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(educourse);
		return renderString(response, baseResponseMsg);
	}
	@RequiresPermissions("edu:course:eduCourse:edit")
	@RequestMapping(value = "updateCourseStatus")
	public String updateCourseStatus(EduCourse eduCourse,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String  courseStatus =request.getParameter("courseStatus") ;
		if(Constant.COURSE_STATUS_NO.equals(courseStatus)){
			//取消发布
			eduCourse.setStatus(Constant.COURSE_STATUS_NO);
		}else{
			//发布
			eduCourse.setStatus(Constant.COURSE_STATUS_YES);
		}
		eduCourseService.save(eduCourse);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:"+Global.getAdminPath()+"/edu/course/eduCourse/?repage";
	}

	@RequiresPermissions("edu:course:eduCourse:edit")
	@RequestMapping(value = "delete")
	public String delete(EduCourse eduCourse, RedirectAttributes redirectAttributes) {
		eduCourseService.delete(eduCourse);
		addMessage(redirectAttributes, "删除课程成功");
		return "redirect:"+Global.getAdminPath()+"/edu/course/eduCourse/?repage";
	}
	@RequestMapping(value = "findList")
	public String findList(EduCourse educourse, Model model, HttpServletRequest request, HttpServletResponse response,RedirectAttributes redirectAttributes){
		BaseResponseMsg baseResponseMsg;
		List<EduCourse> list = eduCourseService.findList(educourse);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(list);
		return renderString(response, baseResponseMsg);
	}

}